package com.eian.boot.security.config;

import cn.dev33.satoken.strategy.SaStrategy;
import com.eian.boot.common.core.factory.YmlPropertySourceFactory;
import com.eian.boot.common.id.IdHelper;
import com.eian.boot.security.handler.SaTokenExceptionHandler;
import jakarta.annotation.PostConstruct;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;

/**
 * sa-token 配置
 *
 * @author alex.meng
 */
@AutoConfiguration
@PropertySource(value = "classpath:common-satoken.yml", factory = YmlPropertySourceFactory.class)
public class SaTokenConfig {
    /**
     * 异常处理器
     */
    @Bean
    public SaTokenExceptionHandler saTokenExceptionHandler() {
        return new SaTokenExceptionHandler();
    }

    @PostConstruct
    public void rewriteSaStrategy() {
        // 重写 Token 生成策略
        SaStrategy.instance.createToken = (loginId, loginType) -> {
            return IdHelper.simpleUuid();
        };
    }
}
